import area from '../../utils/area';
import ValidateForm from "../../utils/validateForm";
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'
import { handleAuthGetUserInfo, handleGetUserPhomeNumber } from '../../utils/wx'
import { isLogin, toLogin, getUserInfo, clearCacheAll } from '../../utils/auth'
import http from '../../utils/http'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        memberId: null,
        id: null,
        contact: null,
        phone: null,
        region: null,
        address: null,
        isDefault: true,

        showPopup: false,
        areaList: [],
        chooseAreaValue: [],

        validate: null,

        userInfo: null,
        type: 'add',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let { type, data } = options;

        let title = '新增收货地址';
        if (type === 'edit') {
            data = JSON.parse(options.data);
            title = '编辑收货地址';
            this.setData({
                type,
                memberId: data.memberId, 
                id: data.id, 
                contact: data.contact, 
                phone: data.phone, 
                address: data.address, 
                isDefault: data.isDefault,
                chooseAreaValue: [data.province, data.city, data.district],
            });
            this.setData({ region: this.data.chooseAreaValue.join(',') })
        }

        // 初始化地区数据
        this.setData({
            areaList: {
                province_list: area.province_list,
                city_list: area.city_list,
                county_list: area.county_list,
            }
        });

        this.initValidate();

        wx.setNavigationBarTitle({ title });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 用户登录信息
        this.setData({ userInfo: getUserInfo() });
    },

    /**
     * 提交表单
     * @param {*} e 
     */
    async formSubmit({detail}) {
        let { id, isDefault, userInfo, type } = this.data
        let params = detail.value;

        if (!this.validate.checkForm(params)) {
            let error = this.validate.errorList[0];

            switch (error.param) {
                case "contact":
                    showNotify(this, error.msg, 'danger'); break;
                case "phone":
                    showNotify(this, error.msg, 'danger'); break;
                case "region":
                    showNotify(this, error.msg, 'danger'); break;
                case "address":
                    showNotify(this, error.msg, 'danger'); break;
            }
            return false;
        }

        let form = Object.assign({}, params, { id, isDefault })
        let region = form.region.split(',')
        form.province = region[0]
        form.city = region[1]
        form.district = region[2]
        form.memberId = userInfo.id
        console.log(form);

        let url = '/api/mall/umsmemberreceiveaddress/save'
        if(type === 'edit') {
            url = '/api/mall/umsmemberreceiveaddress/update'
        }

        let res = await http.post(url, form)
        if(res.status) {
            wx.navigateBack({ delta: 1 });
        }

    },

    initValidate() {
        let rules = {
            contact: {
                required: true
            },
            phone: {
                required: true,
                tel: true
            },
            region: {
                required: true
            },
            address: {
                required: true
            }

        }
        let message = {
            contact: {
                required: '请输入收件人'
            },
            telephone: {
                required: '请输入手机号',
                maxlength: '请输入有效的手机号'
            },
            region: {
                required: '请选择所在地区'
            },
            address: {
                required: '请输入详细地址'
            }
        }
        //实例化当前的验证规则和提示消息
        this.validate = new ValidateForm(rules, message);
    },

    handleShowPopup() {
        this.setData({ showPopup: true });
    },
    handleHidePopup() {
        this.setData({ showPopup: false });
    },

    /**
     * 选中所在地区
     * @param {*} e 
     */
    handleConfirmArea(e) {
        const { values } = e.detail;
        // 选中的地区
        let region = [values[0].name, values[1].name, values[2].name];
        this.setData({ showPopup: false, region });
    },

    /**
     * 选择默认
     * @param {*} e 
     */
    changeIsDefault(e) {
        const { value } = e.target.dataset;
        this.setData({ isDefault: !value })
    },

    /**
     * 编辑地址
     */
    async getAddress() {
        let { id, memberId, contact, phone, region, address, isDefault } = this.data
        let res = await http.get('/api/mall/umsmemberreceiveaddress/info/'+id)
        console.log(id, memberId, contact, phone, region, address, isDefault);
        console.log(res);
        if(res.status) {
            this.setData({  })
        }
    }


})